package cn.glutnn.controller;

import cn.glutnn.entity.Expert;
import cn.glutnn.entity.Result;
import cn.glutnn.entity.User;
import cn.glutnn.entity.dto.LoginDTO;
import cn.glutnn.entity.dto.PasswordDTO;
import cn.glutnn.entity.dto.RegisterDTO;
import cn.glutnn.service.ExpertService;
import cn.glutnn.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (User)表控制层
 *
 * @author msh
 * @since 2024-10-24 16:05:21
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户管理模块")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ExpertService expertService;

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result login(@Valid @RequestBody LoginDTO loginDTO) {
        return userService.login(loginDTO);
    }

    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public Result register(@Valid @RequestBody RegisterDTO registerDTO) {
        return userService.register(registerDTO);
    }

    @Operation(summary = "登录后修改密码")
    @PutMapping("/loginUpdatePassword")
    public Result<User> updatePassword(@Valid @RequestBody PasswordDTO passwordDTO) {
        return userService.updatePassword(passwordDTO);
    }

    @Operation(summary = "登录后查询个人信息")
    @GetMapping("/loginSelectByName")
    public Result<User> selectByName() {
        return userService.selectByName();
    }

    @Operation(summary = "登录后修改个人信息")
    @PutMapping("/loginUpdateByName")
    public Result<User> updateByName(@RequestBody User user) {
        return userService.updateByUsername(user);
    }

    @Operation(summary = "查询所有用户")
    @GetMapping("")
    public Result<List<User>> selectAll() {
        return userService.selectALlList();
    }

    @Operation(summary = "添加用户")
    @PostMapping
    public Result<User> addUser(@Valid @RequestBody User user) {
        return userService.addUser(user);
    }

    @Operation(summary = "根据用户名修改用户")
    @PutMapping("/{userName}")
    public Result<User> updateUser(@PathVariable("userName") String username, @RequestBody User user) {
        return userService.updateUser(username, user);
    }

    @Operation(summary = "根据用户名删除用户")
    @DeleteMapping("/{userName}")
    public Result<User> deleteUser(@PathVariable("userName") String username) {

        return userService.deleteUserByUsername(username);
    }


    @Operation(summary = "根据用户名查询用户", parameters = {
            @Parameter(name = "token", description = "JWT Token", required = true, in = ParameterIn.HEADER)
    })
    @GetMapping("/{userName}")
    public Result<User> selectUserByName(@PathVariable("userName") String userName) {
        return userService.selectUserByName(userName);
    }

    @Operation(summary = "分页查询用户")
    @GetMapping("/search/{pageNum}")
    public Result<Page<User>> selectUserByPage(@PathVariable("pageNum") Integer pageNum) {
        return userService.selectAllUserByPage(pageNum);
    }

    @Operation(summary = "分页查询用户")
    @GetMapping("/search/{pageNum}/{pageSize}")
    public Result<Page<User>> selectUserByPageAndNameAndOther(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize, @RequestBody(required = false) User user) {
        return userService.selectUserByPageAndNameAndOther(pageNum, pageSize, user);
    }


    @Operation(summary = "查询专家详情")
    @GetMapping("/searchexpert")
    public Result<Expert> selectExpert() {
        return expertService.selectExpert();
    }

    @Operation(summary = "查询所有专家")
    @GetMapping("/searchallexpert")
    public Result<List<Expert>> selectAllExpert() {
        return expertService.selectAllExpert();
    }

    @Operation(summary = "分页查询所有商家")
    @GetMapping("/experts")
    public Result<Page<Expert>> selectAllExpertByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                                      @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Expert> page = new Page<>(pageNum, pageSize);
        return Result.success(expertService.page(page));
    }

    @Operation(summary = "根据用户名删除专家详情")
    @DeleteMapping("/deleteexpert/{userName}")
    public Result deleteExpert(@PathVariable("userName") String userName) {
        return expertService.deleteExpertByName(userName);
    }


    @Operation(summary = "修改专家详情")
    @PutMapping("/updateexpert")
    public Result updateExpert(@RequestBody Expert expert) {
        return expertService.updateExpert(expert);
    }


    @Operation(summary = "添加专家详情")
    @PostMapping("/addexpert")
    public Result addExpert(@RequestBody Expert expert) {
        return expertService.addExpert(expert);
    }


    @Operation(summary = "添加或修改专家详情")
    @PostMapping("/addUpdexpert")
    public Result addUpdexpert(@RequestBody Expert expert) {
        return expertService.addUpdexpert(expert);
    }

    // 退出登录
    @Operation(summary = "退出登录")
    @PostMapping("/logout")
    public Result logout() {
        userService.logout();
        return Result.success();
    }
}

